<?php

namespace app\home\form\user;

use app\consts\Code;
use app\customer\FormRequest;
use app\exception\BizException;
use app\model\user\User;

class LoginRequest extends FormRequest
{
    public $username;
    public $password;

    public function rules(): array
    {
        return [
            "username" => "required|email",
            "password" => "required",
        ];
    }

    public function login(): User
    {
        /**@var $user User */
        $user = User::where("username", $this->username)->first();
        if (!$user) {
            throw new BizException(Code::USER_NOT_FOUND);
        }

        //TODO  密码比对

        //TODO 记录日志等

        return $user;
    }
}